package com.joint.base.mp;

import com.fz.us.base.service.memcached.CacheConsts;
import com.fz.us.base.service.memcached.SpyMemcachedClient;
import com.joint.base.bean.EnumManage;
import com.joint.base.bean.SystemConfig;
import com.joint.base.entity.Users;
import com.joint.base.service.UsersService;
import com.joint.base.service.jms.AdvancedNotifyMessageProducer;
import com.joint.base.util.StringUtils;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Created with us2 -> com.fz.us.core.mp.
 * User: min_xu
 * Date: 2014-12-10
 * Time: 17:28
 * 说明：
 */
public class WxEchoMpServiceImpl extends WxMpServiceImpl {

    @Resource
    private UsersService usersService;
    @Resource
    private SpyMemcachedClient spyMemcachedClient;
    @Resource
    private AdvancedNotifyMessageProducer notifyMessageProducer;
    @Resource
    private WxMpInCacheConfigStorage wxMpConfigStorage;
    @Resource
    private SystemConfig systemConfig;

    @Override
    public void accessTokenRefresh() throws WxErrorException {
        String configAppId = wxMpConfigStorage.getAppId();
        String token_cache = CacheConsts.WXMP_ACCESS_TOKEN_STATUS + "#" + configAppId;

//        if(spyMemcachedClient.get(token_cache) == null){
//            spyMemcachedClient.set(token_cache, 24*60*60, "0");
//        }
//        if(spyMemcachedClient.incr(token_cache, 1, 1, 24 * 60 * 60) <= 1){
//            try {
                String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
                        + "&appid=" + wxMpConfigStorage.getAppId()
                        + "&secret=" + wxMpConfigStorage.getSecret()
                        ;
                try {
                    HttpGet httpGet = new HttpGet(url);
                    CloseableHttpResponse response = httpclient.execute(httpGet);
                    String resultContent = new BasicResponseHandler().handleResponse(response);
                    WxError error = WxError.fromJson(resultContent);
                    if (error.getErrorCode() != 0) {
                        throw new WxErrorException(error);
                    }
                    WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
                    wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
                } catch (ClientProtocolException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
//            } finally {
//                spyMemcachedClient.set(token_cache, 24*60*60, "0");
//            }
//        }else{
//            LogUtil.info(token_cache + "现在正在被其他的服务器更新，等待...");
//            // 每隔100ms检查一下是否刷新完毕了
//            while (spyMemcachedClient.incr(token_cache, 1, 1, 24 * 60 * 60) >= 2) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
//            }
            // 刷新完毕了，读取一下cachetoken后就没他什么事儿了
            wxMpConfigStorage.getAccessToken();
//        }
    }

    public void sendFormReadMessage(Users receiver, String content, String target, String targetId) throws WxErrorException {
        target = target.toLowerCase();
        if(StringUtils.isNotEmpty(usersService.openId(receiver))){

            String targetUrl = wxMpConfigStorage.getHttpHost()+"/"+systemConfig.getWebroot()+"/mp/ajax-" + target+"!read.action?keyId="+targetId;
            String text = receiver.getName() + "，您有一条新消息：" + content + "\n\n <a href=\"" + targetUrl + "\">点击链接查看</a>\n";

            WxMpCustomMessage wxMpCustomMessage = WxMpCustomMessage.TEXT()
                    .toUser(usersService.openId(receiver))
                    .content(text)
                    .build();

            notifyMessageProducer.sendQueue(wxMpCustomMessage, EnumManage.NotifyKeyEnum.wxMpCustomMessage.name());
        }
    }



}
